<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/28
 * Time: 16:39
 */
namespace App\Modules\User\Services;
use App\Modules\User\Models\UserAverageDetail;
use Redis;


class UserAverageDetailService {

    /**
     * 获取用户打赏次数及打赏金额
     * @return mixed
     */
    static function getUserRewardsInfo(){
        $rewardTimes = Redis::hget('average_task_info:','rewards_count');
        $rewardTotal = Redis::hget('average_task_info:','rewards_total_fee');
        if($rewardTimes===null || $rewardTotal===null) {
            $info = UserAverageDetail::getAllUsersRewardsInfo();
            $rewardTimes = $info['rewardTimes'];
            $rewardTotal = $info['rewardTotal'];
            Redis::hset('average_task_info:','rewards_count',$rewardTimes);
            Redis::hset('average_task_info:','rewards_total_fee',$rewardTotal);
        }
        return ['rewardTimes'=>$rewardTimes,'rewardTotal'=>$rewardTotal];
    }

    /**
     * 获取用户所有已发布的任务数
     * @param $uid
     * @return mixed
     */
    static function getUserTaskNum()
    {
        $task_num = Redis::hget('average_task_info:', 'task_count');
        if ($task_num === null) {
            $task_num = UserAverageDetail::getAllUserTaskNum();
            Redis::hset('average_task_info:', 'task_count', $task_num);
        }
        return $task_num;
    }

    /**
     * 获取用户感兴趣总数
     */
    static function getUserPickTotal()
    {
        //$interest = 0;
        $interest = Redis::hget('average_task_info:', 'interest_count');
        if ($interest === null) {
            $interest =UserAverageDetail::getAllUserPickTotal();
            Redis::hset('user_task_info:', 'interest_count', $interest);
        }
        return $interest;
    }

    static function getMaxReward()
    {
        $peakReward = Redis::hget('average_task_info:', 'peak_reward');
        if ($peakReward === null) {
            $peakReward = UserAverageDetail::getAllMaxReward();
            Redis::hset('average_task_info:','peak_reward',$peakReward);
        }
        return $peakReward;
    }

    /**
     * 获取用户处理总时长
     * @param $uid
     */
    static function handleAllDuration(){
        $totalTime = Redis::hget('average_task_info:', 'handle_totalTime');
        $totalCount = Redis::hget('average_task_info:', 'handle_totalCount');
        if ($totalTime === null || $totalCount ===null) {
            $info = UserAverageDetail::handleAllDuration();
            $totalTime = $info['totalTime'];
            $totalCount = $info['totalCount'];
            Redis::hset('average_task_info:','handle_totalTime',$totalTime);
            Redis::hset('average_task_info:','handle_totalCount',$totalCount);
        }
        return ['totalTime'=>$totalTime?$totalTime:0,'totalCount'=>$totalCount?$totalCount:0];
    }
}